package com.changgou.controller;

import com.changgou.goods.pojo.Brand;
import com.changgou.service.BrandService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.file.Path;
import java.util.List;

/**
 * @auther LiZiBa
 * @date 2020/7/17 23:09
 * @description:
 **/

@RestController
@RequestMapping(value = "/brand")
/**
 *  跨域：A域名访问B域名
 *  域名、端口、协议不一致则为跨域
 */
@CrossOrigin
public class BrandController {


    @Autowired
    private BrandService brandService;


    /**
     * 分页 + 条件查询实现
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Brand>> findPage(@RequestBody Brand brand,
                                            @PathVariable(value = "page") Integer page,
                                            @PathVariable(value = "size") Integer size){

        // 调用service实现查询
        PageInfo<Brand> brands = brandService.findPage(brand, page, size);

        // 响应结果封装
        return new Result<PageInfo<Brand>>(true, StatusCode.OK, "分页条件查询成功！", brands);
    }

    /**
     * 分页查询实现
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Brand>> findPage(@PathVariable(value = "page") Integer page,
                                    @PathVariable(value = "size") Integer size){

        // 调用service实现查询
        PageInfo<Brand> brands = brandService.findPage(page, size);

        // 响应结果封装
        return new Result<PageInfo<Brand>>(true, StatusCode.OK, "分页查询成功！", brands);
    }

    /**
     * 条件查询
     */
    @PostMapping(value = "/search")
    public Result<List<Brand>> findList(@RequestBody Brand brand){

        // 调用service实现查询
        List<Brand> brands = brandService.findList(brand);

        // 响应结果封装
        return new Result<List<Brand>>(true, StatusCode.OK, "条件搜索查询成功！", brands);
    }


    /**
     * 删除品牌
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable(value = "id")Integer id){

        // 调用service根据id删除品牌
        brandService.delete(id);

        // 响应结果封装
        return new Result(true, StatusCode.OK, "删除品牌成功！");
    }


    /**
     * 修改brand
     */

    @PutMapping(value = "/{id}")
    public Result update(@PathVariable(value = "id") Integer id, @RequestBody Brand brand){

        // ID赋值brand
        brand.setId(id);
        // 调用service实现品牌修改
        brandService.update(brand);
        // 响应结果封装
        return new Result(true, StatusCode.OK, "修改品牌成功！");
    }


    /**
     * 增加brand
     */
    @PostMapping
    public Result add(@RequestBody Brand brand){

        // 调用service增加brand
        brandService.add(brand);

        // 响应结果封装
        return new Result(true, StatusCode.OK, "增加品牌成功！");
    }


    /**
     * 根据主键ID查询
     */
    @GetMapping(value = "/{id}")
    public Result<Brand> findById(@PathVariable(value = "id")Integer id){

        // 根据ID查询单个brand
        Brand brand = brandService.findById(id);

        // 响应结果封装
        return new Result<>(true, StatusCode.OK, "根据ID查询brand成功", brand);
    }


    /**
     * 查询所有品牌Brand
     */
    @GetMapping
    public Result<List<Brand>> findAll(){

        // 查询所有品牌
        List<Brand> brands = brandService.findAll();

        // 响应结果封装
        return new Result<List<Brand>>(true, StatusCode.OK, "查询品牌集合成功", brands);
    }
}
